from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2 as cv


# 均值滤波
def myMean(image,value):
    meanBlur = cv.blur(image,(value,value))
    return meanBlur


#中值滤波
def myMedianBlur(image,value):
    medianBlur = cv.medianBlur(image,value)
    return medianBlur


#高斯滤波
def myGaussBlur(image,value):
    gaussBlur = cv.GaussianBlur(image,(value,value),sigmaX=0,sigmaY=0)
    return gaussBlur


@index_bp.get('/filter_pro')
@index_bp.post('/filter_pro')
@view_logging_required
@permission_required("filter:pro")
def filter_pro():
    if request.method == "GET":
        return render_template('admin/cv/filter.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            file_url = file_read(f)
            pic = cv.imread(file_url)
            choose = int(request.form.get('choose'))
            value = int(request.form.get('value'))
            res_pic = "static/download/filter.jpg"
            if choose == 0:
                out_image = myMean(pic,value)
                cv.imwrite(res_pic,out_image)
            elif choose == 1:
                out_image = myMedianBlur(pic,value)
                cv.imwrite(res_pic,out_image)
            elif choose == 2:
                out_image = myGaussBlur(pic,value)
                cv.imwrite(res_pic,out_image)
            return render_template('admin/cv/filter.html', pic=file_url, res_pic=res_pic)
        else:
            return "请传入正确的文件格式"